#encoding=utf8
'''
Created on Nov 6, 2014

@author: jingyang <jingyang@smalltreemedia.com>
'''
from django.db import models
from django import forms

from core.fields import CustomCharField, CustomTextField
from app_info.models import BaseKeyReq
from core.widgets import CustomTextarea


STORES = (
    ('iTunes App Store', 'iTunes App Store'),
    ('Google Play', 'Google Play'),
    ('Amazon App Store', 'Amazon App Store'),
)


STATUS = (
    ('A', u'Active'),
    ('L', u'Legacy'),
)


class IapType(models.Model):
    name = models.CharField(u'Name', max_length=30)
    store = models.CharField(u'App Store', max_length=50, choices=STORES)
    status = models.CharField(u'Status', max_length=1, choices=STATUS, default='A')

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = u'IAP Type'
        verbose_name_plural = u'IAP Type'


REQUEST_TYPE = (
    ('N', u'Create New IAP Item'),
    ('U', u'Update IAP Price'),
)


REQUEST_STATUS = (
    ('N', u'New'),
    ('F', u'Fulfilled'),
    ('I', u'Invalid'),
    ('C', u'Cancel'),
)


class IapInfo(models.Model):
    app = models.ForeignKey('app_info.App', verbose_name=u'App',
        limit_choices_to={'status': 'V'})
    request_id = models.CharField(u'Request ID', max_length=20, null=True, blank=True)
    create_time = models.DateTimeField(u'Timestamp', auto_now_add=True)
    local_doc = models.CharField(u'IAP Doc (Local)', max_length=1000, null=True,
        blank=True, default='')
    remote_doc = models.CharField(u'IAP Doc (Dropbox)', max_length=1000, null=True,
        blank=True, default='')
    status = models.CharField(u'Request Status', max_length=1, choices=REQUEST_STATUS,
        default='N')
    editor = models.EmailField(u'Editor Email', null=True, blank=True, default='')

    def __unicode__(self):
        return self.request_id

    def get_iap_list(self):
        return self.iap_set.all()

    class Meta:
        verbose_name = u'IAP Request'
        verbose_name_plural = u'IAP Request'
        ordering = ('-status',)


class Iap(models.Model):
    info = models.ForeignKey(IapInfo, verbose_name=u'IAP Request')
    name = models.CharField(u'Name', max_length=200)
    prd_id = CustomCharField(u'Product ID', widget=forms.TextInput(
        attrs={'style': '''width: 450px;'''}), max_length=200)
    type = models.CharField(u'Type', max_length=50)
    price = models.FloatField(u'Price (USD)', default=0.0)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = u'IAP'


class PublicKey(BaseKeyReq):
    pub_key = CustomTextField('Public Key', null=True, blank=True, default='',
        widget=CustomTextarea({'style': '''overflow: hidden; word-wrap: break-word;
            resize: horizontal; width: 800px; height: 200px;'''}))

    def __unicode__(self):
        return 'Public Key of %s' % self.app

    class Meta:
        verbose_name = 'GP Public Key'
        verbose_name_plural = 'GP Public Key'
        ordering = ('-status',)
